﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using System.IO;

public partial class Modules_FlowTrace_TraceDetail : System.Web.UI.Page
{
    IPerf_Flow_TraceHandler traceHandler = new Perf_Flow_TraceManage();
    IPerf_Flow_TraceDtlHandler traceDtlHandler = new Perf_Flow_TraceDtlManage();
    IPerf_Flow_TraceAttachHandler attachHandler = new Perf_Flow_TraceAttachManage();
    IPerf_Flow_StepsHandler stepHandler = new Perf_Flow_StepsManage();
    ISys_UserHandler userHandler = new Sys_UserManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ("download".Equals(Request["method"]))
            {
                string attachId = Request["aid"];
                Perf_Flow_TraceAttachQuery attQuery = attachHandler.GetPerf_Flow_TraceAttachQuery(int.Parse(attachId));
                if (attQuery != null)
                {
                    string path = System.Web.HttpContext.Current.Request.MapPath("~");
                    path = path + "/" + attQuery.Path;
                    path = path.Replace('/', '\\');
                    FileInfo fileInfo = new FileInfo(path);
                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + attQuery.Name);
                    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    Response.AddHeader("Content-Transfer-Encoding", "binary");
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(fileInfo.FullName);
                    Response.Flush();
                    Response.End();
                }
            }
            else
            {
                dataBind();
            }
        }
    }

    private void dataBind()
    {
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            int ID = 0;
            int.TryParse(Request["ID"], out ID);

            if (ID != 0)
            {
                try
                {
                    Perf_Flow_TraceQuery traceQuery = traceHandler.GetPerf_Flow_TraceQuery(ID);
                    if (traceQuery != null)
                    {
                        lblFlowDesc.Text = traceQuery.Description;
                        lblBeginDate.Text = traceQuery.Begin_Time.ToString();
                        lblEndDate.Text = traceQuery.End_Time > traceQuery.Begin_Time ? traceQuery.End_Time.ToString() : "";
                        lblStatus.Text = Enums.GetFlowTraceStatus((Enums.FlowTraceStatus)traceQuery.Status);
                        lblRemark.Text = traceQuery.Remark;

                        if (traceQuery.Current_Step_ID > 0)
                        {
                            Perf_Flow_StepsQuery stepQuery = stepHandler.GetPerf_Flow_StepsQuery(traceQuery.Current_Step_ID);
                            lblCurrentStep.Text = stepQuery.StepName;
                        }
                        else
                        {
                            lblCurrentStep.Text = "开始";
                        }

                        string whereSql = " Flow_Trace_ID = @Flow_Trace_ID";
                        DataTable dt = traceDtlHandler.GetPerf_Flow_TraceDtlList(whereSql, new SqlParameter[] { new SqlParameter("@Flow_Trace_ID", ID) }, " ID asc");
                        gvTraceList.DataSource = dt;
                        gvTraceList.DataBind();
                    }
                }
                catch
                {
                    Jscript.Alert("操作失败!", this.Page);
                }
            }
        }
    }

    protected void gvTraceList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex > -1)
            {
                Perf_Flow_StepsQuery stepQuery = stepHandler.GetPerf_Flow_StepsQuery(int.Parse(e.Row.Cells[0].Text));
                if (stepQuery != null)
                {
                    e.Row.Cells[0].Text = stepQuery.StepName;
                }

                Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(int.Parse(e.Row.Cells[1].Text));
                if (userQuery != null)
                {
                    e.Row.Cells[1].Text = userQuery.Name;
                }

                Enums.YesNoStatus isPass = (Enums.YesNoStatus)int.Parse(e.Row.Cells[3].Text);
                e.Row.Cells[3].Text = Enums.GetYesNoStatus(isPass);

                HtmlGenericControl span = (HtmlGenericControl)e.Row.FindControl("spnAttachList");
                Perf_Flow_TraceAttachQuery[] attachList = attachHandler.GetPerf_Flow_TraceAttachQueryList("Flow_TraceDtl_ID = @Flow_TraceDtl_ID", new SqlParameter[] { new SqlParameter("@Flow_TraceDtl_ID", span.Attributes["idValue"]) }, "ID desc");
                StringBuilder sb = new StringBuilder();
                if (attachList != null && attachList.Length > 0)
                {
                    foreach (Perf_Flow_TraceAttachQuery attQuery in attachList)
                    {
                        sb.Append("<a target='_blank' href=\"?method=download&aid=" + attQuery.ID.ToString() + "\">");
                        sb.Append(attQuery.Name);
                        sb.Append("</a><br/>");
                    }
                }
                span.InnerHtml = sb.ToString();
            }
        }
    }

}
